package com.yulongtian.oneProblemEveryDay.month202211.day20221129;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/
 * 可
 *
 * @author yulongTian
 * @create 2022-11-29 16:54
 */
public class Test06 {
    public static void main(String[] args) {
        int[] groupSizes = {3, 3, 3, 3, 3, 1, 3};
        System.out.println(groupThePeople(groupSizes));

    }

    public static List<List<Integer>> groupThePeople(int[] groupSizes) {
        List<List<Integer>> res = new ArrayList<>();
        HashMap<Integer, List<List<Integer>>> myMap = new HashMap<>();

        for (int i = 0; i < groupSizes.length; i++) {
            int key = groupSizes[i];
            //不包含
            if (!myMap.containsKey(key)) {
                List<List<Integer>> temp = new ArrayList<>();
                List<Integer> temp1 = new ArrayList<>();
                temp1.add(i);
                temp.add(temp1);
                myMap.put(key, temp);
            } else {//包含
                List<List<Integer>> listList = myMap.get(key);
                List<Integer> integers = listList.get(listList.size() - 1);
                if (integers.size() != key) {
                    integers.add(i);
                } else {
                    List<Integer> temp = new ArrayList<>();
                    temp.add(i);
                    listList.add(temp);
                }
            }


        }
        Set<Integer> keySet = myMap.keySet();
        for (Integer integer : keySet) {
            List<List<Integer>> listList = myMap.get(integer);
            res.addAll(listList);
        }


        return res;
    }

}
